<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">一体杆名称：</span>
      <el-input v-model="params.poleName" clearable placeholder="请输入内容" class="search-main" @clear="getList" />

      <span class="search-label">一体杆编号：</span>
      <el-input v-model="params.poleNumber" clearable placeholder="请输入内容" class="search-main" @clear="getList" />

      <span class="search-label">处置状态：</span>
      <el-select v-model="params.handleStatus" @change="getList">
        <el-option label="未派单" :value="0" />
        <el-option label="已派单" :value="1" />
        <el-option label="已接单" :value="2" />
        <el-option label="已完成" :value="3" />
        <el-option label="全部" :value="null" />
      </el-select>

      <el-button type="primary" class="search-btn" @click="getList">查询</el-button>
    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="list" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column label="一体杆名称" prop="poleName" />
        <el-table-column label="一体杆编号" prop="poleNumber" />
        <el-table-column label="故障类型" prop="errorType" />
        <el-table-column label="处置状态" prop="handleStatus" :formatter="formatStatus" />
        <el-table-column label="告警时间" prop="warningTime" />

        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text" :disabled="true">派单</el-button>
            <el-button size="mini" type="text" @click="detail(scope.row.id)">详情</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页结构 -->
    <div class="page-container">
      <el-pagination layout="total, sizes, prev, pager, next" :total="total" :current-page="params.page"
        :page-sizes="[2, 4, 6, 8, 10]" :page-size="params.pageSize" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
import { getRodWarnListAPI } from '@/api/warn'
import { delRodWarndAPI } from '@/api/warn'

export default {
  data() {
    return {
      list: [], // 表格数据源
      total: 0, // 总条数
      params: { // 查询参数
        page: 1, // 当前页码
        pageSize: 6, // 每页显示数量
        handleStatus: null // 处置状态
      }
    }
  },

  mounted() {
    this.getList()
  },

  methods: {
    // 获取列表数据
    getList() {
      getRodWarnListAPI(this.params).then(({ data }) => {
        this.list = data.rows // 表格数据源
        this.total = data.total // 总条数
      })
    },

    // 跳转查看详情
    detail(id) {
      this.$router.push('/pole/warning/?id=' + id)
    },

    // 删除数据
    del(id) {
      this.$confirm('你确认要删除吗？', '删除', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRodWarndAPI(id).then(() => {
          this.getList() // 刷新表格数据
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 2000
          }) // 弹出成功提示
        }).catch(() => {
          this.$message({
            message: '删除失败',
            type: 'error',
            duration: 2000
          }) // 弹出失败提示
        })
      }).catch(() => {
        console.log(2) // 取消删除操作
      })
    },

    // 处理每页显示数量变化
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize // 更新每页显示数量
      this.getList() // 刷新表格数据
    },

    // 处理页码变化
    handleCurrentChange(page) {
      this.params.page = page // 更新当前页码
      this.getList() // 刷新表格数据
    },

    // 格式化处置状态
    formatStatus(row) {
      switch (row.handleStatus) {
        case 0:
          return '未派单';
        case 1:
          return '已派单';
        case 2:
          return '已接单';
        case 3:
          return '已完成';
        default:
          return '';
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgba(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}
</style>
